// API使用示例
import { 
  authApi, 
  userApi, 
  roleApi, 
  permissionApi, 
  menuApi, 
  fileApi, 
  logApi, 
  systemApi 
} from './index'

// 认证相关示例
export const authExamples = {
  // 用户登录
  async loginExample() {
    try {
      const credentials = {
        username: 'admin',
        password: '123456'
      }
      const response = await authApi.login(credentials)
      console.log('登录成功:', response)
      return response
    } catch (error) {
      console.error('登录失败:', error)
      throw error
    }
  },

  // 获取当前用户信息
  async getCurrentUserExample() {
    try {
      const response = await authApi.getCurrentUser()
      console.log('当前用户信息:', response)
      return response
    } catch (error) {
      console.error('获取用户信息失败:', error)
      throw error
    }
  }
}

// 用户管理示例
export const userExamples = {
  // 获取用户列表
  async getUsersExample() {
    try {
      const params = {
        page: 1,
        pageSize: 10,
        keyword: '',
        status: 'active'
      }
      const response = await userApi.getUsers(params)
      console.log('用户列表:', response)
      return response
    } catch (error) {
      console.error('获取用户列表失败:', error)
      throw error
    }
  },

  // 创建用户
  async createUserExample() {
    try {
      const userData = {
        username: 'newuser',
        email: 'newuser@example.com',
        password: '123456',
        roleIds: [1, 2]
      }
      const response = await userApi.createUser(userData)
      console.log('创建用户成功:', response)
      return response
    } catch (error) {
      console.error('创建用户失败:', error)
      throw error
    }
  },

  // 更新用户
  async updateUserExample(userId) {
    try {
      const userData = {
        email: 'updated@example.com',
        status: 'active'
      }
      const response = await userApi.updateUser(userId, userData)
      console.log('更新用户成功:', response)
      return response
    } catch (error) {
      console.error('更新用户失败:', error)
      throw error
    }
  }
}

// 角色管理示例
export const roleExamples = {
  // 获取角色列表
  async getRolesExample() {
    try {
      const response = await roleApi.getRoles()
      console.log('角色列表:', response)
      return response
    } catch (error) {
      console.error('获取角色列表失败:', error)
      throw error
    }
  },

  // 分配角色权限
  async assignRolePermissionsExample(roleId) {
    try {
      const permissionIds = [1, 2, 3, 4]
      const response = await roleApi.assignRolePermissions(roleId, permissionIds)
      console.log('分配权限成功:', response)
      return response
    } catch (error) {
      console.error('分配权限失败:', error)
      throw error
    }
  }
}

// 权限管理示例
export const permissionExamples = {
  // 获取权限树
  async getPermissionTreeExample() {
    try {
      const response = await permissionApi.getPermissionTree()
      console.log('权限树:', response)
      return response
    } catch (error) {
      console.error('获取权限树失败:', error)
      throw error
    }
  },

  // 检查权限
  async checkPermissionExample() {
    try {
      const permissionCode = 'user:create'
      const response = await permissionApi.checkPermission(permissionCode)
      console.log('权限检查结果:', response)
      return response
    } catch (error) {
      console.error('权限检查失败:', error)
      throw error
    }
  }
}

// 菜单管理示例
export const menuExamples = {
  // 获取用户菜单
  async getUserMenusExample() {
    try {
      const response = await menuApi.getUserMenus()
      console.log('用户菜单:', response)
      return response
    } catch (error) {
      console.error('获取用户菜单失败:', error)
      throw error
    }
  }
}

// 文件管理示例
export const fileExamples = {
  // 上传文件
  async uploadFileExample(file) {
    try {
      const onProgress = (progressEvent) => {
        const percentCompleted = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        )
        console.log('上传进度:', percentCompleted + '%')
      }
      
      const response = await fileApi.uploadFile(file, onProgress)
      console.log('文件上传成功:', response)
      return response
    } catch (error) {
      console.error('文件上传失败:', error)
      throw error
    }
  },

  // 获取文件列表
  async getFilesExample() {
    try {
      const params = {
        page: 1,
        pageSize: 20,
        keyword: '',
        fileType: 'all'
      }
      const response = await fileApi.getFiles(params)
      console.log('文件列表:', response)
      return response
    } catch (error) {
      console.error('获取文件列表失败:', error)
      throw error
    }
  }
}

// 日志管理示例
export const logExamples = {
  // 获取日志列表
  async getLogsExample() {
    try {
      const params = {
        page: 1,
        pageSize: 50,
        startDate: '2024-01-01',
        endDate: '2024-12-31',
        level: 'info',
        operationType: 'create'
      }
      const response = await logApi.getLogs(params)
      console.log('日志列表:', response)
      return response
    } catch (error) {
      console.error('获取日志列表失败:', error)
      throw error
    }
  },

  // 导出日志
  async exportLogsExample() {
    try {
      const params = {
        startDate: '2024-01-01',
        endDate: '2024-12-31',
        format: 'excel'
      }
      const response = await logApi.exportLogs(params)
      
      // 创建下载链接
      const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
      const url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = url
      link.download = 'logs.xlsx'
      link.click()
      window.URL.revokeObjectURL(url)
      
      console.log('日志导出成功')
    } catch (error) {
      console.error('日志导出失败:', error)
      throw error
    }
  }
}

// 系统设置示例
export const systemExamples = {
  // 获取系统信息
  async getSystemInfoExample() {
    try {
      const response = await systemApi.getSystemInfo()
      console.log('系统信息:', response)
      return response
    } catch (error) {
      console.error('获取系统信息失败:', error)
      throw error
    }
  },

  // 更新系统设置
  async updateSystemSettingsExample() {
    try {
      const settings = [
        { key: 'site_name', value: '通用管理系统' },
        { key: 'site_description', value: '一个功能强大的管理系统' },
        { key: 'max_file_size', value: '10MB' }
      ]
      const response = await systemApi.batchUpdateSettings(settings)
      console.log('系统设置更新成功:', response)
      return response
    } catch (error) {
      console.error('系统设置更新失败:', error)
      throw error
    }
  }
} 